import { _decorator, Button, director, Label, Sprite } from 'cc';
import { UIComponent } from '../../../../Framework/Scripts/Managers/UIComponent';
import { EventMsgKes } from '../../Config/ResConfig';
import { FRIENDOPT } from '../../Config/GameCfg';
import { FriendJoinNotify, FriendJoinOperateRes } from '../../../AssetPackage/Props/Game/DT_PK';
const { ccclass, property } = _decorator;

@ccclass('GameHYInvite_Ctr')
export class GameHYInvite_Ctr extends UIComponent {
    spHead: Sprite;
    labId: Label;

    isClick: boolean;
    protected onLoad(): void {
        this.InitUI();
    }

    start() {
        this.InitData();
    }

    protected onDestroy(): void {
        this.unRegisterEvent();
    }

    private InitUI(){
        this.registerEvent();

        this.labId = this.ViewComponent('labId',Label);
        this.spHead = this.ViewComponent('head/spHead',Sprite); 
        this.setHeadMat(this.spHead);
        this.AddButtonListener('btn1',this,this.OnBtn1Click);
        this.AddButtonListener('btn2',this,this.OnBtn2Click);
    }

    private InitData(){
        this.isClick = true;
    }

    private registerEvent(){
        globalThis.gameMgr._eventTarget.on(EventMsgKes.msgFriendJoinOperateRes,this.onFriendJoinOperateRes,this);
    }

    private unRegisterEvent(){
        globalThis.gameMgr._eventTarget.off(EventMsgKes.msgFriendJoinOperateRes,this.onFriendJoinOperateRes,this);
    }

    //接受
    private OnBtn1Click(btn:Button){
        if(this.isClick == false){
            return;
        }
        this.isClick = false;
        globalThis.gameMgr._GameHallMsg.FriendJoinOperateReq(FRIENDOPT.AGREE);
    }

    //拒绝
    private OnBtn2Click(btn:Button){
        if(this.isClick == false){
            return;
        }
        this.isClick = false;
        globalThis.gameMgr._GameHallMsg.FriendJoinOperateReq(FRIENDOPT.REFUSE);
    }

    //用户操作返回
    private onFriendJoinOperateRes(data:FriendJoinOperateRes){
        console.log('=========onFriendJoinOperateRes===========',data)
        this.isClick = true;

        let code = data.code || 0;
        if(code == 0){ //加入成功

        }else{ //拒绝
            this.node.destroy();
        }
    }

    public setData(data:FriendJoinNotify){
        if(data){
            this.labId.string = `好友ID:${data.userId}`;
            this.LoadRemoteImage(data.user.avator,this.spHead);
        }
    }
}


